#pragma once

#include "NetworkManager.h"
#include <QVector>
#include "Connection.h"


class QTcpServer;

namespace Networking
{
    //typedef QPair<int, ConnectionPtr> PlayerConn; // pairs of player's ID & socket

    class Server : public NetworkManager
    {
        Q_OBJECT
    public:
        Server(quint16 port = 7777);

    public slots:
        void SlotProcessNewConnection();
        void SlotDispatchMsg(const QString& msg);
        void SlotDisconnect(){}

        void SlotPlayerlistUpdate(const QString&);
        void SlotGameStarted();
        void SlotGameEnded();

        void SlotBroadcastAll(const QString& msg); // broadcast message to all established connections;
        void SlotBroadcast(const QString& msg); // broadcast message only to players;

    private slots:
        void parseMessage(const QString&, int sender);

    private:
        quint16 m_port; //server's port
        QTcpServer * m_serverSocket;

        QVector<ConnectionPtr> m_players; //for connections which where added to playerlist
        QVector<ConnectionPtr> m_connections; //for all connected sockets
        QString m_playerlistMsg;

        static int m_playersNum;

        bool isPlayer(ConnectionPtr conn) const;
    };
}
